package com.dlc.shop.sys.platform.controller;


import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dlc.shop.common.annotation.SysLog;
import com.dlc.shop.common.bean.Quick100;
import com.dlc.shop.common.bean.SysConfig;
import com.dlc.shop.common.config.Constant;
import com.dlc.shop.common.i18n.I18nMessage;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.service.SysConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author chenlin
 * @DateTime: 2020/3/19 8:29
 * @description: 支付配置、文件上传配置、短信配置、快递配置、小程序配置、公众号配置
 */
@Tag(name = "系统配置")
@RestController
@RequestMapping("/sys/pconfig")
@RequiredArgsConstructor
public class SysPconfigController {


    private final SysConfigService sysConfigService;
    @Value("${yami.expose.permission:}")
    private Boolean permission;

    @SysLog("获取配置信息")
    @GetMapping("/info/{key}")
    @Operation(summary = "获取配置信息" , description = "获取配置信息")
    @Parameter(name = "key", description = "参数名" )
    public ServerResponseEntity<String> info(@PathVariable("key")String key){
        if (Objects.equals(key, Constant.PAY_SETTLEMENT_CONFIG)) {
            return ServerResponseEntity.success(sysConfigService.getValue(key));
        }
        if (BooleanUtil.isFalse(permission)) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.success(sysConfigService.getValue(key));
    }

    @GetMapping("/quick100")
    @Operation(summary = "获取快递100配置", description = "获取快递100配置")
    public ServerResponseEntity<Boolean> getQuick100() {
        Quick100 quick100 = sysConfigService.getSysConfigObject(Constant.QUICK100_CONFIG, Quick100.class);
        return ServerResponseEntity.success(quick100.getIsPrint());
    }

    @SysLog("保存配置")
    @PostMapping("/save")
    @Operation(summary = "保存配置" , description = "保存配置")
    public ServerResponseEntity<Void> save(@RequestBody @Valid SysConfig sysConfig){
        if (BooleanUtil.isFalse(permission)) {
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.operate.auth"));
        }
        String paramValue = sysConfig.getParamValue();
        String paramKey = sysConfig.getParamKey();
        long count = sysConfigService.count(new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getParamKey,paramKey));
        if (count>0){
            sysConfigService.updateValueByKey(paramKey,paramValue);
        }else {
            sysConfigService.save(sysConfig);
        }
        sysConfigService.removeSysConfig(paramKey);
        return ServerResponseEntity.success();
    }

    @PostMapping("/saveDeliveryConfig")
    @Operation(summary = "保存物流查询配置信息" , description = "保存物流查询配置信息")
    public ServerResponseEntity<Void> saveDeliveryConfig(@RequestBody @Valid SysConfig sysConfig) {
        if (BooleanUtil.isFalse(permission)) {
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.operate.auth"));
        }
        List<String> invalidKeys = sysConfigService.saveDeliveryConfig(sysConfig);
        invalidKeys.forEach(key -> {
            sysConfigService.removeSysConfig(key);
        });
        return ServerResponseEntity.success();
    }
}
